package com.business.action.admin.database;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.business.util.JavaBeanBuilder;

import net.sf.rose.file.util.FileUtils;
import net.sf.rose.jdbc.PageBean;
import net.sf.rose.jdbc.bean.FieldBean;
import net.sf.rose.jdbc.bean.TableBean;
import net.sf.rose.jdbc.service.MetaService;
import net.sf.rose.jdbc.service.Service;

@Controller
@RequestMapping("/admin/class")
public class ClassBeanAction {

	private static List<ClassTree> allClassTree = null;

	/**
	 * 列出所有表结构信息
	 */
	@ResponseBody
	@RequestMapping("/all.do")
	public PageBean all() {
		PageBean page = new PageBean();
		MetaService ms = Service.getMeta();
		page.setData(ms.getTableList());
		return page;
	}

	/**
	 * 列出所有表结构信息，是带树型结构的层级关系
	 */
	@ResponseBody
	@RequestMapping("/list.do")
	public PageBean list() {
		ClassTree node = null;
		ClassTree root = null;
		allClassTree = new ArrayList<>();
		MetaService ms = Service.getMeta();
		List<TableBean> tlist = ms.getTableList();
		Map<String, ClassTree> rootMap = new LinkedHashMap<>();
		// 处理所有表
		for (TableBean bean : tlist) {
			String name = bean.getClassName();
			String[] arr = StringUtils.split(name, '.');
			// 先生成根结点
			root = rootMap.get(arr[0]);
			if (root == null) {
				root = new ClassTree();
				root.setName(arr[0]);
				rootMap.put(arr[0], root);
			}
			// 所有的树枝节点
			node = root;
			for (int i = 1; i < arr.length - 1; i++) {
				boolean flag = false;
				for (ClassTree tree : node.getChildren()) {
					if (tree.getName().equals(arr[i])) {
						node = tree;
						flag = true;
						break;
					}
				}
				if (!flag) {
					ClassTree child = new ClassTree();
					child.setName(arr[i]);
					node.getChildren().add(child);
					node = child;
				}
			}
			// 叶子结点
			boolean flag = false;
			for (ClassTree tree : node.getChildren()) {
				if (tree.getName().equals(arr[arr.length - 1])) {
					// 合并属性
					// 表或视图名合并
					tree.addTable(bean);
					flag = true;
					break;
				}
			}
			if (!flag) {
				ClassTree child = new ClassTree();
				child.setName(arr[arr.length - 1]);
				child.addTable(bean);
				allClassTree.add(child);
				node.getChildren().add(child);
			}
		}
		// 处理所有视图
		List<TableBean> vlist = ms.getViewList();
		for (TableBean bean : vlist) {
			String name = bean.getClassName();
			String[] arr = StringUtils.split(name, '.');
			root = rootMap.get(arr[0]);
			if (root == null) {
				root = new ClassTree();
				root.setName(arr[0]);
				rootMap.put(arr[0], root);
			}
			node = root;
			for (int i = 1; i < arr.length - 1; i++) {
				boolean flag = false;
				for (ClassTree tree : node.getChildren()) {
					if (tree.getName().equals(arr[i])) {
						node = tree;
						flag = true;
						break;
					}
				}
				if (!flag) {
					ClassTree child = new ClassTree();
					child.setName(arr[i]);
					node.getChildren().add(child);
					node = child;
				}
			}
			boolean flag = false;
			for (ClassTree tree : node.getChildren()) {
				if (tree.getName().equals(arr[arr.length - 1])) {
					// 合并属性
					// 表或视图名合并
					tree.addTable(bean);
					flag = true;
					break;
				}
			}
			if (!flag) {
				ClassTree child = new ClassTree();
				child.setName(arr[arr.length - 1]);
				child.addTable(bean);
				allClassTree.add(child);
				node.getChildren().add(child);
			}
		}
		// 第一层根结点
		List<ClassTree> list = new ArrayList<>();
		for (ClassTree n : rootMap.values()) {
			list.add(n);
		}
		PageBean page = new PageBean();
		page.setData(list);
		return page;
	}

	/**
	 * 保存单个表结构信息
	 */
	@ResponseBody
	@RequestMapping("/toJavaFile.do")
	public PageBean toJavaFile(HttpServletRequest request) {
		PageBean page = new PageBean();
		JavaBeanBuilder jb = new JavaBeanBuilder();
		File path = FileUtils.JAVA_SOURCE_PATH;
		for (ClassTree tree : allClassTree) {
			jb.build(path, tree);
			jb.write();
		}
		return page;
	}

	public class ClassTree {

		/** 节点名称 */
		private String name = null;

		/** 子树 */
		private List<ClassTree> children = new ArrayList<>();

		/** 表名视图列表 */
		private List<TableBean> tableList = new ArrayList<>();

		/** 字段列表 */
		private List<FieldBean> fields = new ArrayList<>();

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public List<ClassTree> getChildren() {
			return children;
		}

		public void setChildren(List<ClassTree> children) {
			this.children = children;
		}

		public List<TableBean> getTableList() {
			return tableList;
		}

		public void setTableList(List<TableBean> tableList) {
			this.tableList = tableList;
		}

		public List<FieldBean> getFields() {
			return fields;
		}

		public void setFields(List<FieldBean> fields) {
			this.fields = fields;
		}

		public void addTable(TableBean table) {
			String name = table.getClassName();
			if (name == null) {
				return;
			}
			tableList.add(table);
			boolean flag = false;
			for (FieldBean f : table.getFields()) {
				flag = false;
				String fieldName = f.getFieldName();
				for (FieldBean field : fields) {
					if (field.getFieldName().equalsIgnoreCase(fieldName)) {
						flag = true;
						break;
					}
				}
				if (!flag) {
					fields.add(f);
				}
			}
		}
	}

}
